*Selection-Probit_99.do

 clear matrix

*
*log using "C:\Selfemployment\Self-Employment\selfemployment.smcl", replace
clear
set mem 300000
set matsize 800

set more off
**************************************************************************************
**************************************************************************************
*****             Merge Person and Economic Familiy Data Files                   *****
**************************************************************************************
**************************************************************************************
set more off


use "/Users/dgordon/Desktop/Self-Employment/slid99ef.dta", clear


sort puchid25
save "/Users/dgordon/Desktop/Self-Employment/slid99ef.dta", replace
*load sum of individual for investment and capital gains
*insheet using "C:\Selfemployment\Self-Employment\slid99_sum.txt", clear

*rename inva42 Tinva42
*rename capgn42 Tcapgn42
*sort puchid25
*save "C:\Selfemployment\Self-Employment\slid99_sum.dta", replace
*use "C:\Selfemployment\Self-Employment\slid99ef.dta", clear

*merge puchid25 using "C:\Selfemployment\Self-Employment\slid99_sum.dta"
*drop _merge
*** droping non unique/duplicate observations in a household in ef file

gen time=_n
tsset time
gen D1=0
replace D1=1 if  puchid25 == l.puchid25
replace D1=1 if  f.puchid25 == puchid25

 
sort puchid25
save "/Users/dgordon/Desktop/Self-Employment/slid99ef_c.dta", replace



use "/Users/dgordon/Desktop/Self-Employment/slid99per.dta", clear
sort puchid25

merge puchid25 using "/Users/dgordon/Desktop/Self-Employment/slid99ef_c.dta"    

drop if D1==1 
**************************************************************************************
**************************************************************************************
***                       Create/Clean-Up Variables for the Analysis               ***
**************************************************************************************
**************************************************************************************


**********Create Self-Employment Dummy from Major Source of Income********************
**********This is the dependent variable for the probit regression********************
*****
**********Building number and age of kids

gen selfemp=1 if(majri42==3 & fmse42==0)
* & nbjbs28==1 &  fllprt1==1)

replace selfemp=0 if majri42==2 & fmse42==0 
*& nfmse42==0)
* & nbjbs28==1   &  fllprt1==1)
 
drop if selfemp==.
 
sum selfemp 


******************Adjust Total Income to 1000s*****************************************

gen btincome=ttinc42/1000

 
 

*********************Adjust Earnings to $1000s*****************************************

replace earng42=earng42/1000


*********************Adjust Investment Income to $1000s*******************************

replace inva27= inva27/1000

*********************Adjust Taxable Capital Gains to $1000s****************************
 
replace capgn27= capgn27/1000

*********************Adjust Age 10s****************************
 gen age = ecage26
replace ecage26= ecage26/10

**********Create Age Squared for Nonlinear Effect of Age******************************

gen age2=ecage26*ecage26

**********Create Health Status Dummy (1 if disabled 0 otherwise)********************

gen health=1 if(disabs26==1)
replace health=0 if(disabs26==2)
 

drop if health==.
   
 

**********Create Gender Variable******************************************************

gen male=1 if ecsex99==1
replace male=0 if ecsex99==2

drop if male==. 
 
*********Create Marital Status Dummy (1 if married/common-law 0 otherwise)*********

gen married=1 if(marst26==1 | marst26==2)
replace married=0 if(married==.)
 

**************** education variables
* years of education in variable yrschl18
gen edsq = yrschl18^2
*****
 


 
**********Create Children Dummies 
 

* trying to approximate the number of kids in the household
*      
gen kids =0
replace kids = hhsz25-nbempd27 if (married==0 & agyfm27<=18)
replace kids = hhsz25-2 if (married==1 & nbempd27==1& agyfm27<=18)
replace kids = hhsz25-nbempd27 if (married==1 & nbempd27>1& agyfm27<=18)
replace kids=0 if kids<0
*  assigning ages to kids starting from youngest kid in family
drop if agyfm27<=0
gen kidage1=0
replace kidage1=agyfm27 if kids>=1
gen kidage2=0 
replace kidage2=agyfm27+2 if kids>=2
gen kidage3=0
replace kidage3=agyfm27+3 if kids>=3
gen kidage4=0
replace kidage4=agyfm27+4 if kids>=4
gen kidage5=0
replace kidage5=agyfm27+5 if kids>=5
gen kidage6=0
replace kidage6=agyfm27+6 if kids>=6
gen kidage7=0
replace kidage7=agyfm27+7 if kids>=7

sum selfemp
*******generate spousal earnings; set earnings to 1000000 so does not activate a 
* tax benefit for non married persons **note if more than 2 in HH the family earnings minus
* person earnings is shared equally for all in HH except person***
** note spouce earnsing set to 1000000 unless reference person is married common law and
* at least 2 or more people is employed in househould, 

gen spoucearn=1000000
replace spoucearn = (earng27-(earng42*1000))/(nbempd27-1) if married==1 & nbempd27>=2
replace spoucearn = (earng27-(earng42*1000)) if married==1 & nbempd27==1 & hhsz25>1

replace spoucearn = earng27 if married==1 & earng42<0 & nbempd27==2 & hhsz25>1
replace spoucearn = earng27/(nbempd27-1) if married==1 & earng42<0 & nbempd27>=2

replace spoucearn = 0 if married==1 & spoucearn<=0 
***********

**************************Clean-Up Immigrant Status Variable**************************
 
*drop if(immst15==6 | immst15==7 | immst15==8 | immst15==9)

gen immigrant=1 if(immst15==1)
replace immigrant=0 if(immigrant==.)


*************************Create Occupation Dummies***********************************

drop if(s91g2e6==96 | s91g2e6==97 | s91g2e6==98 | s91g2e6==99)

gen whtcol=1 if(s91g2e6==1 | s91g2e6==2 | s91g2e6==3 | s91g2e6==4 | s91g2e6==5 | s91g2e6==6 | s91g2e6==7 | s91g2e6==8 | s91g2e6==9 | s91g2e6==10  | s91g2e6==12)
replace whtcol=0 if(whtcol==.)

gen blucol=1 if(s91g2e6==18 | s91g2e6==19 | s91g2e6==20 | s91g2e6==21 | s91g2e6==22 | s91g2e6==23 | s91g2e6==24 | s91g2e6==25)
replace blucol=0 if(blucol==.)

gen service=1 if(s91g2e6==11 | s91g2e6==13 | s91g2e6==14 | s91g2e6==15 | s91g2e6==16 | s91g2e6==17)
replace service=0 if(service==.)
 
***************************Create Residence Dummies***********************************

drop if(pvreg25==96 | pvreg25==97 | pvreg25==98 | pvreg25==99)

gen newfoundland=1 if(pvreg25==10)
replace newfoundland=0 if(newfoundland==.)

gen pei=1 if(pvreg25==11)
replace pei=0 if( pei==.)

gen novascotia=1 if(pvreg25==12)
replace novascotia=0 if (novascotia==.)

gen newbrunswick=1 if(pvreg25==13)
replace newbrunswick=0 if (newbrunswick==.)

gen quebec=1 if(pvreg25==24)
replace quebec=0 if(quebec==.)

gen ontario=1 if(pvreg25==35)
replace ontario=0 if(ontario==.)

gen manitoba=1 if(pvreg25==46)
replace manitoba=0 if(manitoba==.)

gen saskatchewan=1 if(pvreg25==47)
replace saskatchewan=0 if (saskatchewan==.)

gen alberta=1 if(pvreg25==48)
replace alberta=0 if(alberta==.)

gen bc=1 if(pvreg25==59)
replace bc=0 if(bc==.)

 
**************************Create Percent of Income Due to Earnings*******************


gen pctincearn = earng42/ttinc42*100

 

**************************Clean-Up Weeks Unemployed**********************************
 
drop if  wksuem28==96 | wksuem28==97 | wksuem28==98 | wksuem28==99

*********
*build RD variable for 1999**************
gen rd = 0
replace rd = (127/92.9)*100 if newfoundland==1 & year==1999
replace rd = (26/92.9)*100  if pei==1 & year==1999
replace rd = (342/92.9)*100 if novascotia==1 & year==1999
replace rd = (165/92.9)*100 if newbrunswick==1 & year==1999
replace rd = (4885/92.9)*100 if quebec==1 & year==1999
replace rd = (8115/92.9)*100 if ontario==1 & year==1999
replace rd = (384/92.9)*100 if manitoba==1 & year==1999
replace rd = (323/92.9)*100 if saskatchewan==1 & year==1999
replace rd = (1164/92.9)*100 if alberta==1 & year==1999
replace rd = (1290/92.9)*100 if bc==1 & year==1999

**********************************************************************************************
 
* years or experience is weird
drop if yrxfte11==96 |yrxfte11==97 | yrxfte11==98 | yrxfte11==99

replace earng42=ln(earng42)
drop if earng42==.
 
 *drop if major activity is not working
drop if mjacg26~=1
* drop if not fulltime
*drop if fllprt1~=1
*drop if unpaid family worker
drop if clwkr1==2
*drop if not head of hh mjieh26 & rmjig26  from Key.dta file
*drop if mjieh26~=1
*keep only head of hh and spouce  3 child 4 other
*drop if rmjig26==3 | rmjig26==4
 
* more than one person in HH earning $
gen DhhE =0
replace DhhE= 1 if nbear27>=2
sum selfemp if selfemp==1  


**********************************************************************************************
*gen fd1 = f.D1**********                             BEGIN THE ANALYSIS                                     ***************
**********************************************************************************************
**********************************************************************************************

 

******************Summary Statistics for Variables Used In Analsis*********************
noisily sum  yrxfte11 earng42 inva27 capgn27 pctincearn yrschl18 ecage26 age2 health male married kids immigrant wksuem28 blucol whtcol service newfoundland pei novascotia newbrunswick quebec ontario manitoba saskatchewan alberta bc  if(selfemp==0)
noisily sum  yrxfte11 earng42 inva27 capgn27 pctincearn yrschl18 ecage26 age2 health male married kids immigrant wksuem28 blucol whtcol service newfoundland pei novascotia newbrunswick quebec ontario manitoba saskatchewan alberta bc  if(selfemp==1)
***
 


******************Probit Model of Choice of Self-Employment****************************
*************(Note: White Collar and Ontario are omitted dummies)*********************
*************(Note: Children is the identification restriction)*****************************
 
 
 

*****************
************
 
*********************



noisily probit selfemp inva27 capgn27  yrschl18 edsq ecage26 age2 wksuem28 health male married kids blucol service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc, robust


noisily heckman earng42  yrschl18 ecage26 age2 wksuem28 health male married blucol service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc,  select (selfemp= inva27 capgn27  yrschl18 edsq ecage26 age2  wksuem28 health male married kids immigrant blucol service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc)   mills(invmills) 
*predict btearningSE, ycond
*gen varh=e(sigma)^2

*replace earng42 = exp(earng42)
*gen btSE = exp(btearningSE)
*regress earng42 btSE, noconstant
*gen ao = _b[btSE]
*replace earng42 = ln(earng42)
*het
reg earng42   yrschl18  ecage26 age2 wksuem28 health male married blucol  service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc invmills if(selfemp==1)

predict btearningSE 
predict btearningSE_df
gen sigma2 = e(rss)/e(df_r)



gen btSE2 = btearningSE^2
predict rse if selfemp==1 , resid
** gen variance and se of regression
*
egen ao = total(exp(rse)) 
sum ao if selfemp==1
replace ao = ao/1389

replace rse = ln(rse^2)
reg rse btearningSE btSE2 if selfemp==1
*btSE2
predict ser
replace rse = exp(ser)
*****************************


gen rse_df = sqrt(sigma2*rse)

replace btearningSE=ao*exp(btearningSE)



* 

*****************************
 
 **building inversemills for employed 
gen Employ =0
replace Employ =1 if selfemp ==0  

noisily heckman earng42  yrschl18 ecage26 age2 wksuem28 health male married blucol service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc,  select(Employ= inva27 capgn27  yrschl18 edsq ecage26 age2 wksuem28 health male married kids immigrant  blucol  service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc)  mills(invmills2) 
*predict btearningE, ycond
*gen var1 = e(sigma)^2
**********
*
*replace earng42 = exp(earng42)
*gen btE = exp(btearningE)
*regress earng42 btE, noconstant
*gen aoo = _b[btE]
*replace earng42 = ln(earng42)

reg earng42   yrschl18  ecage26 age2 wksuem28 health male married blucol  service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc invmills2 if(selfemp==0)
predict btearningE 
predict rsee if selfemp==0, residual
*need predicted y not logy
egen aoo = total(exp(rsee)) 
sum aoo if selfemp==0
replace aoo = aoo/20664

*gen var1= e(rss)/e(df_r)

*replace btearningE=exp(var1/2)*exp(btearningE)
replace btearningE=aoo*exp(btearningE)


*correcting prediction for log wage Wooldridge p212



*

*noisily reg earng42   yrschl18   ecage26 age2 wksuem28 health male married blucol  service newfoundland pei novascotia newbrunswick quebec manitoba saskatchewan alberta bc invmills2 if(selfemp==0), robust
*est store wsols99
*predict btearningE 

* sum btearningE btearningSE



 
*********************Create Income Difference Variable*********************************

replace btearningSE = btearningSE*1000
 
sum btearningSE rse
replace btearningE = btearningE*1000
 

*********************Adjust Earnings to $1000s*****************************************
 replace earng42 =exp(earng42)
replace earng42=earng42*1000
sum  rse btearningSE btearningE earng42

*replace rse = rse*1000
   

*********************Adjust Investment Income to $1000s*******************************

replace inva27= inva27*1000

*********************Adjust Taxable Capital Gains to $1000s****************************

replace capgn27= capgn27*1000
sum inva27 capgn27 

****Building incomedif********
 
*gen incomedif2 =  ln((btearningSE)/(btearningE))
 
*
*
gen other =inva42+capgn42+rspwi42+pen42+oasgi42+cpqpp42+uiben42+sapis42+ottxm42+alimo42
gen other2=inva27+capgn27+rspwi27+pen27+oasgi27+cpqpp27+uiben27+sapis27+ottxm27+alimo27
gen netother = other2-other

 gen Idif =(btearningSE+inva27+capgn27+uiben42+sapis42+wkrcp42+ottxm42)/(btearningE+inva27+capgn27+uiben42+sapis42+wkrcp42+ottxm42)
drop if Idif<=0
sum Idif 
gen incomedif = ln(Idif)
 

****

*********************Adjust Age 10s****************************

replace ecage26= ecage26*10
gen date=year 
summ capgn27 inva27 earng42

noisily sum btearningSE btearningE incomedif
*******************define mean income starting at zero
gen mean_inc = 0


*estout reducedprobit99 seols99 wsols99 using "C:\Selfemployment\Self-Employment\99Estimates.xls", cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(r2 N, labels(R-squared)) varlabels(_cons Constant) delimiter(,) replace 
******
keep mean_inc date ao other other2 rse rse_df DhhE  inctx42 fditx42 pvitx42 inctx27 wkrcp42 spoucearn btearningE btearningSE btearningSE_df incomedif  year pucpid26 puchid25 selfemp inva27 inva42 capgn27   yrschl18 ecage26 age2 wksuem28 health male married   kids immigrant blucol   service newfoundland pei novascotia newbrunswick ontario quebec manitoba saskatchewan alberta bc earng42  wksuem28 udpd42  medx42 rppc42 rspwi42 pen42 oasgi42 cpqpp42 uiben42 capgn27 capgn42 sapis42 ottxm42 cpqpp42 alimo42 rppc42 chfed42 ccar42 alip42 year  kidage1-kidage7 rd  yrxfte11  pctincearn whtcol  


******SEstdfrisk
save "/Users/dgordon/Desktop/Self-Employment/99analysis", replace

*******************************************************************************************
*******************************************************************************************
********                 Saving Data to Input into CTaCS                                              *******
*******************************************************************************************
*******************************************************************************************

use "/Users/dgordon/Desktop/Self-Employment/99analysis", clear

noisily sum udpd42  medx42 rppc42 rspwi42 pen42 oasgi42 cpqpp42 uiben42 capgn27 sapis42 ottxm42 inva27

rename  pucpid26 id
generate province = 1 if(newfoundland==1)
replace province = 2 if(pei==1)
replace province = 3 if(novascotia==1)
replace province = 4 if(newbrunswick==1)
replace province = 5 if(quebec==1) 
replace province = 6 if(ontario==1)
replace province = 7 if(manitoba==1)
replace province = 8 if(saskatchewan==1)
replace province = 9 if(alberta==1)
replace province = 10 if(bc==1)
gen age = ecage26
rename health disabled
rename udpd42 dues
rename medx42 medexp
rename rppc42 rppcon 
rename btearningE earn
rename btearningSE self
rename rspwi42 rrspinc
rename pen42 peninc 
rename oasgi42 oasinc
rename cpqpp42 cqpinc
rename uiben42 uiinc 
rename capgn42 cginc
rename sapis42 sainc
gen othinc = ottxm42+alimo42
gen othded = chfed42+ccar42+alip42
*rename inva42 intinc
rename inva42 dvdinc
rename wkrcp42 wcinc
rename spoucearn equivsp

keep id year province male age disabled dues medexp rppcon earn rrspinc peninc oasinc cqpinc uiinc cginc sainc othinc wcinc dvdinc equivsp kidage1-kidage7
save "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/99WS", replace

******ctacsWS99.d0***********************************************************************************
* C-TaCS - The Canadian Tax and Credit Simulator
* Build 2007-2
* 
* Program name: interface.do
* Program purpose: provides the interface to the end user
* Program updated: October 23, 2007 by km
***********************************************************************************

* set stata parameters.
clear 
*/* Note:  set this so that there is sufficient memory to handle your input file */
set more off

********************************
*
* There are five items to be assigned below. For each, replace what is in the quotation marks with the desired directory/filename.
* 1. assign the root directory.
* 2. assign the input/output directory.
* 3. assign the name for the logfile.
* 4. assign the name of the input dataset.
* 5. assign the name of the output dataset.
*
********************************

* Assign directories
global ctacs "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/"  		
*/****1****/
global inout "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/"   
*/****2****/

* Assign log file
global logfile "interface.log"                                   
* /****3****/

* Assign input and output files
* global indata "ctacs-in"                                      /****4****/
global indata "99WS"                                    
*  /****4****/
global outdata "ctacs-99ws"                
*                    /****5****/

********************************
*
* Nothing else needs to be done below this point.
*
*******************
* load the programs
do $ctacs/files/loadprogs.do

*Run the ptogram
ctacs 

use  "/Users/dgordon/Desktop/Self-Employment/99analysis", clear

rename  pucpid26 id
generate province = 1 if(newfoundland==1)
replace province = 2 if(pei==1)
replace province = 3 if(novascotia==1)
replace province = 4 if(newbrunswick==1)
replace province = 5 if(quebec==1)
replace province = 6 if(ontario==1)
replace province = 7 if(manitoba==1)
replace province = 8 if(saskatchewan==1)
replace province = 9 if(alberta==1)
replace province = 10 if(bc==1)
gen age = ecage26
rename health disabled
rename udpd42 dues
rename medx42 medexp
rename rppc42 rppcon 
rename btearningE earn
rename btearningSE self
rename rspwi42 rrspinc
rename pen42 peninc 
rename oasgi42 oasinc
rename cpqpp42 cqpinc
rename uiben42 uiinc 
rename capgn42 cginc
rename sapis42 sainc
gen othinc = ottxm42+alimo42
gen othded = chfed42+ccar42+alip42
*rename inva42 intinc
rename inva42 dvdinc
rename wkrcp42 wcinc
rename spoucearn equivsp


keep id year province male age disabled dues medexp rppcon self rrspinc peninc oasinc cqpinc uiinc cginc sainc othinc wcinc dvdinc equivsp kidage1-kidage7


save "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/99SE", replace // need predicted income from simulation NOT predicted income on average from equation
*******************************************
* C-TaCS - The Canadian Tax and Credit Simulator
* Build 2007-2
* 
* Program name: interface.do
* Program purpose: provides the interface to the end user
* Program updated: October 23, 2007 by km
***********************************************************************************

* set stata parameters.
clear 
*set mem 200m  /* Note:  set this so that there is sufficient memory to handle your input file */
set more off

********************************
*
* There are five items to be assigned below. For each, replace what is in the quotation marks with the desired directory/filename.
* 1. assign the root directory.
* 2. assign the input/output directory.
* 3. assign the name for the logfile.
* 4. assign the name of the input dataset.
* 5. assign the name of the output dataset.
*
********************************

* Assign directories
global ctacs "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1"  		/****1****/
global inout "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/"   /****2****/

* Assign log file
global logfile "interface.log"                                    /****3****/

* Assign input and output files
* global indata "ctacs-in"                                      /****4****/
global indata "99SE"                                      /****4****/
global outdata "ctacs-99se"                                    /****5****/

********************************
*
* Nothing else needs to be done below this point.
*
********************************

* load the programs
do $ctacs/files/loadprogs.do

* Run the program
ctacs


use  "/Users/dgordon/Desktop/Self-Employment/99analysis", clear


* loop for denisty function
forv i = 2(1)1001 {

use  "/Users/dgordon/Desktop/Self-Employment/99analysis", clear



gen btearningSE_df`i'  = rnormal(btearningSE_df, rse_df)
replace btearningSE_df`i' = (exp(btearningSE_df`i'))*1000



*****
replace mean_inc =(mean_inc+btearningSE_df`i')
save  "/Users/dgordon/Desktop/Self-Employment/99analysis", replace

rename  pucpid26 id
generate province = 1 if(newfoundland==1)
replace province = 2 if(pei==1)
replace province = 3 if(novascotia==1)
replace province = 4 if(newbrunswick==1)
replace province = 5 if(quebec==1)
replace province = 6 if(ontario==1)
replace province = 7 if(manitoba==1)
replace province = 8 if(saskatchewan==1)
replace province = 9 if(alberta==1)
replace province = 10 if(bc==1)
gen age = ecage26
rename health disabled
*udp42 union dues
rename udpd42 dues
rename medx42 medexp
rename rppc42 rppcon 
rename btearningE earn
rename btearningSE_df`i'  self
rename rspwi42 rrspinc
rename pen42 peninc 
rename oasgi42 oasinc
rename cpqpp42 cqpinc
rename uiben42 uiinc 
rename capgn42 cginc
rename sapis42 sainc
gen othinc = ottxm42+alimo42
gen othded = chfed42+ccar42+alip42
*rename inva42 intinc
rename inva42 dvdinc
rename wkrcp42 wcinc
rename spoucearn equivsp


keep id year province male age disabled dues medexp rppcon self rrspinc peninc oasinc cqpinc uiinc cginc sainc othinc wcinc dvdinc equivsp kidage1-kidage7


save "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/99SE_df`i'", replace

global logfile "interface.log"                                    /****3****/

* Assign input and output files
* global indata "ctacs-in"                                      /****4****/
global indata "99SE_df`i'"                                      /****4****/
global outdata "ctacs-99SE_df`i'"                                    /****5****/

********************************
*
* load the programs
do $ctacs/files/loadprogs.do

* Run the program
ctacs


use "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/ctacs-99SE_df`i'.dta", clear

rename id pucpid26 
rename totfedtax totfedtaxSE_df`i' 
rename totprovtax totprovtaxSE_df`i' 
rename tottax tottaxSE_df`i'

keep pucpid26 totfedtaxSE_df`i' totprovtaxSE_df`i' tottaxSE_df`i' 

save "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df`i'.dta", replace

}


*clean up files
forv i = 2(1)1001 {
erase "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/99SE_df`i'.dta"
}
***********************
use "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1", clear

replace E_T =0 
save "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1", replace

forv i = 2(1)1001 {
use "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1", clear

sort pucpid26

*
merge using "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df`i'"
tab _merge
drop _merge
save "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1", replace
*clean up files
erase "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df`i'.dta"
erase "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/ctacs-99SE_df`i'.dta"
*gen expected taxes

replace E_T = (E_T+tottaxSE_df`i')
drop tottaxSE_df`i' totfedtaxSE_df`i' totprovtaxSE_df`i'

save "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1", replace
}

replace E_T = E_T/1000
save "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1", replace
****
**********


**tax on mean income from simulations
use  "/Users/dgordon/Desktop/Self-Employment/99analysis", clear
replace mean_inc = mean_inc/1000

save  "/Users/dgordon/Desktop/Self-Employment/99analysis", replace

rename  pucpid26 id
generate province = 1 if(newfoundland==1)
replace province = 2 if(pei==1)
replace province = 3 if(novascotia==1)
replace province = 4 if(newbrunswick==1)
replace province = 5 if(quebec==1)
replace province = 6 if(ontario==1)
replace province = 7 if(manitoba==1)
replace province = 8 if(saskatchewan==1)
replace province = 9 if(alberta==1)
replace province = 10 if(bc==1)
gen age = ecage26
rename health disabled
rename udpd42 dues
rename medx42 medexp
rename rppc42 rppcon 
rename btearningE earn
rename mean_inc self
rename rspwi42 rrspinc
rename pen42 peninc 
rename oasgi42 oasinc
rename cpqpp42 cqpinc
rename uiben42 uiinc 
rename capgn42 cginc
rename sapis42 sainc
gen othinc = ottxm42+alimo42
gen othded = chfed42+ccar42+alip42
*rename inva42 intinc
rename inva42 dvdinc
rename wkrcp42 wcinc
rename spoucearn equivsp


keep id year province male age disabled dues medexp rppcon self rrspinc peninc oasinc cqpinc uiinc cginc sainc othinc wcinc dvdinc equivsp kidage1-kidage7


save "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/99SE_M", replace // need predicted income from simulation NOT predicted income on average from equation
*******************************************
* C-TaCS - The Canadian Tax and Credit Simulator
* Build 2007-2
* 
* Program name: interface.do
* Program purpose: provides the interface to the end user
* Program updated: October 23, 2007 by km
***********************************************************************************

* set stata parameters.
clear 
*set mem 200m  /* Note:  set this so that there is sufficient memory to handle your input file */
set more off

********************************
*
* There are five items to be assigned below. For each, replace what is in the quotation marks with the desired directory/filename.
* 1. assign the root directory.
* 2. assign the input/output directory.
* 3. assign the name for the logfile.
* 4. assign the name of the input dataset.
* 5. assign the name of the output dataset.
*
********************************

* Assign directories
global ctacs "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1"  		/****1****/
global inout "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/"   /****2****/

* Assign log file
global logfile "interface.log"                                    /****3****/

* Assign input and output files
* global indata "ctacs-in"                                      /****4****/
global indata "99SE_M"                                      /****4****/
global outdata "ctacs-99se_m"                                    /****5****/

********************************
*
* Nothing else needs to be done below this point.
*
********************************

* load the programs
do $ctacs/files/loadprogs.do

* Run the program
ctacs

*******

use "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/ctacs-99se_m.dta", clear

rename id pucpid26 
rename totfedtax totfedtaxSE_M 
rename totprovtax totprovtaxSE_M 
rename tottax tottaxSE_M

keep pucpid26 totfedtaxSE_M totprovtaxSE_M tottaxSE_M

save "/Users/dgordon/Desktop/Self-Employment/99ctacsse_m.dta", replace
**
use "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/ctacs-99ws.dta", clear

rename id pucpid26 
rename totfedtax totfedtaxWS 
rename totprovtax totprovtaxWS 
rename tottax tottaxWS

keep pucpid26 totfedtaxWS totprovtaxWS tottaxWS

save "/Users/dgordon/Desktop/Self-Employment/99ctacsWS.dta", replace
**
use "/Users/dgordon/Desktop/Self-Employment/Ctacs/CTaCS-2008-1/inout/ctacs-99se.dta", clear

rename id pucpid26 
rename totfedtax totfedtaxSE 
rename totprovtax totprovtaxSE 
rename tottax tottaxSE

keep pucpid26 totfedtaxSE totprovtaxSE tottaxSE

save "/Users/dgordon/Desktop/Self-Employment/99ctacsSE.dta", replace
****


***********************
use "/Users/dgordon/Desktop/Self-Employment/99analysis", clear

sort pucpid26
*drop _merge
*
merge using "/Users/dgordon/Desktop/Self-Employment/99ctacsSE_df1"
tab _merge
drop _merge
**************

merge using "/Users/dgordon/Desktop/Self-Employment/99ctacsSE"
tab _merge
drop _merge

merge using "/Users/dgordon/Desktop/Self-Employment/99ctacsse_m"
tab _merge
drop _merge


merge using "/Users/dgordon/Desktop/Self-Employment/99ctacsWS"
tab _merge
drop _merge

**************Summarize Predicted Earnings and Predicted Taxes**********************

sum  btearningSE btearningE 
bysort selfemp:  sum earng42

gen tax_df=(E_T)/(tottaxSE_M)
gen D_df =0
replace D_df=1 if tax_df<1

gen Tax_ratio=tottaxSE_M/((mean_inc+other)-tottaxSE_M)
gen taxdif_df=  ln(tax_df)*(Tax_ratio)


gen taxdif_df2 = (tax_df-1)*(Tax_ratio) 

gen DT_df=D_df*taxdif_df
gen DT_df2 =D_df*taxdif_df2

**** upside and downside convexity
gen taxdif_dfU =0
gen taxdif_dfD =0
replace taxdif_dfU = taxdif_df if tax_df>1
replace taxdif_dfD = taxdif_df if tax_df<=1
* Keeping only variables to be used in final   model

keep date  taxdif_dfU taxdif_dfD E_T mean_inc other Tax_ratio other2 rse taxdif_df2 DhhE D_df DT_df DT_df2 inctx42 fditx42 pvitx42 inctx27 wkrcp42 spoucearn btearningE btearningSE  incomedif  year pucpid26 puchid25 selfemp inva27 inva42 capgn27   yrschl18 ecage26 age2 wksuem28 health male married   kids immigrant blucol   service newfoundland pei novascotia newbrunswick ontario quebec manitoba saskatchewan alberta bc earng42  wksuem28 udpd42 medx42 rppc42 rspwi42 pen42 oasgi42 cpqpp42 uiben42 capgn27 capgn42 sapis42 ottxm42 cpqpp42 alimo42 rppc42 chfed42 ccar42 alip42 year  kidage1-kidage7 rd yrxfte11  pctincearn whtcol incomedif taxdif_df tax_df tottaxSE_M tottaxSE tottaxWS   
 
 
 *****aggregate unemployment
gen Unemp = 0.76 // canada
replace Unemp = 0.93 if quebec==1
replace Unemp = 0.93 if quebec==1
replace Unemp = 0.63 if ontario==1
replace Unemp = 0.56 if manitoba==1
replace Unemp = 0.60 if saskatchewan==1
replace Unemp = 0.57 if alberta==1
replace Unemp = 0.83 if bc==1

 *****pay roll taxes
gen ptax = 13.54 // Nfld
replace ptax = 12.51 if pei==1
replace ptax = 11.18 if novascotia ==1
replace ptax = 11.02 if newbrunswick ==1
replace ptax = 15.58 if quebec ==1
replace ptax = 11.59 if ontario==1
replace ptax = 11.94 if manitoba==1
replace ptax = 10.36 if saskatchewan==1
replace ptax = 8.21 if alberta==1
replace ptax = 10.77 if bc==1

**Income taxes
gen itax = 21.34 // Nfld
replace itax = 21.46 if pei==1
replace itax = 21.4 if novascotia ==1
replace itax = 20.72 if newbrunswick ==1
replace itax = 24.96 if quebec ==1
replace itax = 24.11 if ontario==1
replace itax = 21.49 if manitoba==1
replace itax = 21.61 if saskatchewan==1
replace itax = 23.79 if alberta==1
replace itax = 23.71 if bc==1



save "/Users/dgordon/Desktop/Self-Employment/99analysis", replace

 

